﻿using ConfExpert.Forms.Base;
using ConfExpert.Utility;
using ConfExpert.Utility.Peripheral;
using DevExpress.XtraEditors;
using DevTeam;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Windows.Forms;

namespace ConfExpert.Forms.Depo
{
    public partial class TekrarTaranacaklar : XForm
    {
        public TekrarTaranacaklar()
        {
            InitializeComponent();
        }

        private void TekrarTaranacaklar_Load(object sender, EventArgs e)
        {
            this.Text = "Tekrar Taranması Gereken Kumaşlar";

            gridView1.Init("GetTekrarTaranacaklar");
            gridView1.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Bottom;
            gridView1.AddSummary();
            gridView1.MakeReadOnly("ProductId");
            gridView1.MakeRowSelectable();
            gridView1.BestFitColumns();

            btnGeciciTara.ItemClick += btnGeciciTara_ItemClick;
            btnNormalTara.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(this.btnNormalTara_ItemClick);
            btnBuyukTara.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(this.btnBuyukTara_ItemClick);
            gridView1.FocusedRowChanged += gridView1_FocusedRowChanged;
            gridView1_FocusedRowChanged(null, null);
        }

        void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            object o = gridView1.GetFocusedRowCellValue("Scan");
            btnGeciciTara.Enabled = (o == null);

            if (gridView1.IsNewItemRow(gridView1.FocusedRowHandle))
            {
                btnGeciciTara.Enabled = false;
            }
        }


        Scanner.PhotosTable photosTable = new Scanner.PhotosTable();

        //void UpdPhoto7(params object[] p)
        //{
        //    using (SqlConnection sqlConn = new SqlConnection(Session.DataManager.ConnectionString))
        //    {
        //        sqlConn.Open();
        //        SqlCommand sqlCmd = sqlConn.CreateCommand();
        //        sqlCmd.CommandText = "UpdPhoto7";
        //        sqlCmd.CommandType = CommandType.StoredProcedure;
        //        SqlParameter[] parameters = new SqlParameter[3];
        //        parameters[0] = new SqlParameter("@tbl", SqlDbType.Structured);
        //        parameters[0].Value = p[0];
        //        parameters[1] = new SqlParameter("@DeviceID", SqlDbType.VarChar);
        //        parameters[1].Value = p[1];
        //        parameters[2] = new SqlParameter("@TempScanInfo", SqlDbType.Int);
        //        parameters[2].Value = p[2];
        //        sqlCmd.Parameters.AddRange(parameters);
        //        sqlCmd.ExecuteNonQuery();
        //    }
        //}

        private void btnGeciciTara_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Tara(true, false);
        }

        private void Tara(bool gecici = false, bool big = false)
        {
            gridView1.EndEditing();
            DataRow drx = gridView1.GetDataRow(gridView1.FocusedRowHandle);
            int ProductId = drx["ProductId"].ToInt();

            if (ProductId == 0)
                return;

            try
            {
                this.Cursor = Cursors.WaitCursor;

                Image scanned = Scanner.Scan(big);    //kumaş için sabitlendi

                if (scanned != null)
                {
                    photosTable = new Scanner.PhotosTable();
                    DataRow dr = photosTable.AddRow(ProductId, scanned, big);
                    AppSession.Lookups.V_Kumas.FindByProductId(ProductId).PhotoMikro = (byte[])dr["Photo1"];
                    gridView1.InvalidateRow(gridView1.FocusedRowHandle);
                    ExecuteNonQuery("UpdPhoto7", photosTable.Table, Environment.MachineName, gecici ? 1 : 0); // 0: "NoTempScan"  , 1: "GeciciTara"
                    gridView1.RefreshDataSource("GetTekrarTaranacaklar");

                    GC.Collect();

                    Notify("Tarama İşlemi Başarıyla Gerçekleşti.");
                }

            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        private void btnNormalTara_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Tara(false, false);

        }

        private void btnBuyukTara_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            bool big = (XtraMessageBox.Show("Bu seçenek, rapor boyu 10 cm.'den büyük kumaşlar içindir. Rapor boyu 10 cm.'den büyük mü?", "Dikkat", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes);

            Tara(false, true);
        }

    }
}
